package com.vieewer.btdemo.wifimain;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.webkit.DownloadListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.vieewer.btdemo.R;
import com.vieewer.btdemo.base.BaseActivity;
import org.jetbrains.annotations.Nullable;

public class WebViewActivity extends BaseActivity {


    // Intent传递过来的值
    private String title, urls;
    // 是否能右滑结束Activity
    private boolean isMove = false;
    // 当前WebView打开的地址(拼接的)
    private String mIsNowUrl;

    WebView webView;



    public static final int FROM_RECHARGE = 1;
    private int fromFlag;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        initView();
    }

    protected void initView() {
        webView=findViewById(R.id.webview);
        urls=getIntent().getStringExtra("WebviewUrl");

//        android.webkit.WebSettings ws = mBindingView.webview.getSettings();
        webView.getSettings().setJavaScriptEnabled(true);//开启JavaScript支持
        webView.getSettings().setBlockNetworkImage(false);//解决图片不加载
        webView.getSettings().setDomStorageEnabled(true);//解决网页点击失效
//        mBindingView.webview.setDownloadListener(new MyWebViewDownLoadListener());//文件下载

// 设置可以支持缩放
        webView.getSettings().setSupportZoom(true);
// 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(true);
//扩大比例的缩放
        webView.getSettings().setUseWideViewPort(true);
        //隐藏缩放工具
        webView.getSettings().setDisplayZoomControls(false);
//自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setLoadWithOverviewMode(true);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {//解决图片不加载
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }


        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
//加载网页面开始时触发，一般加个进度条
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //加载网页面结束时触发，一般去掉进度条
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                // 接受所有网站的证书
                handler.proceed();
            }
        });
        webView.loadUrl(urls);

    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            finish();
        }
    }

    private class MyWebViewDownLoadListener implements DownloadListener {

        @Override
        public void onDownloadStart(String s, String s1, String s2, String s3, long l) {
            Uri uri = Uri.parse(s);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }


}
